package com.hongtong.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hongtong.auto.JSONAuthentication;
import com.hongtong.entity.TeCur;
import com.hongtong.entity.TeQa;
import com.hongtong.entity.TeTask;
import com.hongtong.mapper.TeCurMapper;
import com.hongtong.mapper.TeTaskMapper;
import com.hongtong.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hongtong
 * @since 2022-04-13
 */
@RestController
@RequestMapping("/te-task")
public class TeTaskController extends JSONAuthentication {
    @Autowired
    TeTaskMapper teTaskMapper;
    @Autowired
    TeCurMapper teCurMapper;

    @ApiOperation(value = "添加作业")
    @PostMapping("/add")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestBody TeTask teTask) throws  Exception{

            if(teTaskMapper.insert(teTask)>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            }else{
                Result error = Result.error().message("添加失败");
                this.WriteJSON(request, response, error);
            }
        }
    @ApiOperation(value = "删除作业")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=teTaskMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改作业内容")
    @PostMapping("/upd")
    public void upd(HttpServletRequest request, HttpServletResponse response, @RequestBody TeTask teTask ) throws Exception{

        int i=teTaskMapper.updateById(teTask);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看全部布置的作业")
    @PostMapping("/lookTask")
    public void lookTask(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current ) throws Exception{
        Page<TeTask> page=new Page<>(size,current);
        Page<TeTask> page1=teTaskMapper.selectPage(page,null);
        List<TeTask> records = page1.getRecords();
        for (TeTask a :records){
            a.setTeCur(teCurMapper.selectById(a.getCurId()));
        }
          int total=teTaskMapper.selectCount(null);
        if(records.size()>0){
            Result ok = Result.ok().data("test",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "根据课程名查询布置的作业")
    @PostMapping("/lookTaskBYName")
    public void lookTaskBYName(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current ,@RequestParam("ClassName")String name) throws Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("name",name);
        TeCur teCur = teCurMapper.selectOne(wrapper);
        QueryWrapper wrapper1=new QueryWrapper();
        wrapper1.eq("cur_id",teCur.getId());

        Page<TeTask> page=new Page<>(size,current);
        Page<TeTask> page1=teTaskMapper.selectPage(page,wrapper1);
        List<TeTask> records = page1.getRecords();
        for (TeTask a :records){
            a.setTeCur(teCurMapper.selectById(a.getCurId()));
        }
        int total=teTaskMapper.selectCount(wrapper1);
        if(records.size()>0){
            Result ok = Result.ok().data("test",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查看布置的作业根据作业id")
    @PostMapping("/lookTaskById")
    public void lookTaskById(HttpServletRequest request, HttpServletResponse response,@RequestParam("id") int id) throws Exception{
        TeTask teTask = teTaskMapper.selectById(id);
        teTask.setTeCur(teCurMapper.selectById(teTask.getCurId()));
        if(teTask!=null){
            Result ok = Result.ok().data("test",teTask);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }


}

